package com.lightboat.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lightboat.common.utils.DateUtils;
import com.lightboat.system.domain.CrmCustomer;
import com.lightboat.system.domain.request.CustomerTopRequest;
import com.lightboat.system.mapper.CrmCustomerMapper;
import com.lightboat.system.service.ICrmCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户Service业务层处理
 *
 * @author lightboat
 * @date 2025-08-14
 */
@Service
@RequiredArgsConstructor
public class CrmCustomerServiceImpl extends ServiceImpl<CrmCustomerMapper, CrmCustomer> implements ICrmCustomerService {

    private final CrmCustomerMapper crmCustomerMapper;

    /**
     * 查询客户
     *
     * @param customerId 客户主键
     * @return 客户
     */
    @Override
    public CrmCustomer selectCrmCustomerByCustomerId(Long customerId) {
        return crmCustomerMapper.selectCrmCustomerByCustomerId(customerId);
    }

    /**
     * 查询客户列表
     *
     * @param crmCustomer 客户
     * @return 客户
     */
    @Override
    public List<CrmCustomer> selectCrmCustomerList(CrmCustomer crmCustomer) {
        return crmCustomerMapper.selectCrmCustomerList(crmCustomer);
    }

    /**
     * 新增客户
     *
     * @param crmCustomer 客户
     * @return 结果
     */
    @Override
    public int insertCrmCustomer(CrmCustomer crmCustomer) {
        crmCustomer.setCreateTime(DateUtils.getNowDate());
        return crmCustomerMapper.insert(crmCustomer);
    }

    /**
     * 修改客户
     *
     * @param crmCustomer 客户
     * @return 结果
     */
    @Override
    public int updateCrmCustomer(CrmCustomer crmCustomer) {
        crmCustomer.setUpdateTime(DateUtils.getNowDate());
        return crmCustomerMapper.updateById(crmCustomer);
    }

    /**
     * 批量删除客户
     *
     * @param customerIds 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerByCustomerIds(Long[] customerIds) {
        return crmCustomerMapper.deleteCrmCustomerByCustomerIds(customerIds);
    }

    /**
     * 删除客户信息
     *
     * @param customerId 客户主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerByCustomerId(Long customerId) {
        return crmCustomerMapper.deleteCrmCustomerByCustomerId(customerId);
    }

    @Override
    public int customerTop(CustomerTopRequest request) {
        LambdaUpdateWrapper<CrmCustomer> wrapper = Wrappers.<CrmCustomer>lambdaUpdate();
        wrapper.eq(CrmCustomer::getCustomerId, request.getCustomerId());
        CrmCustomer crmCustomer = BeanUtil.copyProperties(request, CrmCustomer.class);
        if("1".equals(request.getTopStatus())){
            crmCustomer.setTopTime(DateUtils.getNowDate());
        }else {
            wrapper.set(CrmCustomer::getTopTime, null);
            wrapper.set(CrmCustomer::getTopType, null);
        }
        return baseMapper.update(crmCustomer, wrapper);
    }
}
